import sys

from PySide6.QtGui import QGuiApplication, QAction, Qt, QPixmap, QIcon, QImage, QActionGroup
from PySide6.QtWidgets import QMainWindow, QMenu, QApplication, QToolBar, QStatusBar, QLabel
import resources.resources_rc  # 导入图片资源不可去


class Main_Window_Ui(QMainWindow):
    """ 主窗口的UI类
        负责生成主窗口的UI界面和按钮的等控件
        为初始化布局做准备
    """
    def __init__(self):
        super(Main_Window_Ui, self).__init__()

        # 初始化图片
        self.pix_red = QPixmap.fromImage(QImage(':/red_button.png'))
        self.pix_green = QPixmap.fromImage(QImage(':/green_button.png'))
        self.icon_logo = QIcon(':/logo.ico')

        # 配置窗口
        self.setWindowTitle('上位机-v1.1.1')
        self.setWindowIcon(self.icon_logo)
        self.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu)

        # # 获取屏幕尺寸并将程序设为3/4的屏幕大小
        screen = QGuiApplication.primaryScreen().availableGeometry()
        self.resize(300, 200)

        # 设置全屏显示
        # self.setWindowState(Qt.WindowMaximized)

        # self.resize(int(screen.width() * 3 / 4), int(screen.height() * 3 / 4))
        # self.setWindowState(Qt.WindowMaximized)

        # 获取真实分辨率
        # screen = QGuiApplication.primaryScreen().availableGeometry()
        # print(screen)

        # 将程序放到屏幕中心
        size = self.geometry()
        self.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.height()) / 2))

        # 创建菜单栏、工具栏、状态栏
        self.create_menu_bar()
        self.create_tool_bar()
        self.create_status_bar()

    def create_menu_bar(self):
        """ 创建菜单栏和相关按钮 """
        # 创建菜单栏
        self.menu_bar = self.menuBar()

        # 文件相关
        self.file_menu = self.menu_bar.addMenu('文件')
        self.create_new_config_action = QAction('新建配置')
        self.open_config_action = QAction('打开配置')
        self.open_config_var_action = QAction('修改配置')
        self.reload_config_var_action = QAction('重新加载')
        self.export_file_action = QAction('导出头文件')
        self.load_default_action = QAction('加载缺省值')
        self.file_menu.addAction(self.create_new_config_action)
        self.file_menu.addAction(self.open_config_action)
        self.file_menu.addAction(self.open_config_var_action)
        # self.file_menu.addAction(self.reload_config_var_action)
        self.file_menu.addSeparator()
        self.file_menu.addAction(self.export_file_action)
        self.file_menu.addSeparator()
        self.file_menu.addAction(self.load_default_action)

        # 清除记录相关
        # self.record_menu = self.menu_bar.addMenu('清除记录')
        # self.clear_console_record_action = QAction('清除控制台记录')
        # self.clear_tab_table_record_action = QAction('清除中心页面记录')
        # self.clear_dock_scope_record_action = QAction('清除scope页面')
        # self.record_menu.addAction(self.clear_console_record_action)
        # self.record_menu.addAction(self.clear_tab_table_record_action)
        # self.record_menu.addAction(self.clear_dock_scope_record_action)

        # 视图相关
        self.view_menu = self.menu_bar.addMenu('视图')
        self.console_view_action = QAction('控制台视图')
        self.console_view_action.setCheckable(True)
        self.console_view_action.setChecked(True)
        # self.dock_tree_view_action = QAction('参数配置')
        # self.dock_tree_view_action.setCheckable(True)
        self.dock_scope_view_action = QAction('scope视图')
        self.dock_scope_view_action.setCheckable(True)
        self.dock_scope_view_action.setChecked(True)
        self.view_menu.addAction(self.console_view_action)
        # self.view_menu.addAction(self.dock_tree_view_action)
        self.view_menu.addAction(self.dock_scope_view_action)

        # 管理相关
        self.manage_menu = self.menu_bar.addMenu('管理')
        self.communication_manage_action = QAction('通讯配置')
        self.netCard_config_manage_action = QAction('网卡配置')
        self.reset_tcp_action = QAction('重置tcp连接')
        self.program_update_action = QAction('远程升级')
        self.upload_struct_data_action = QAction('下载数据')
        self.com_online_action = QAction('上线')
        self.com_online_action.setCheckable(True)
        self.com_offline_action = QAction('下线')
        self.save_host_data_action = QAction('保存host数据')

        # 修改日志等级
        self.modify_logging_Level_menu = QMenu('修改日志等级')
        self.logging_level_action_group = QActionGroup(self.modify_logging_Level_menu)
        self.logging_Level_DEBUG_action = QAction('DEBUG')
        self.logging_Level_DEBUG_action.setCheckable(True)
        self.logging_level_action_group.addAction(self.logging_Level_DEBUG_action)
        self.logging_Level_INFO_action = QAction('INFO')
        self.logging_Level_INFO_action.setCheckable(True)
        self.logging_level_action_group.addAction(self.logging_Level_INFO_action)
        self.logging_Level_WARNING_action = QAction('WARNING')
        self.logging_Level_WARNING_action.setCheckable(True)
        self.logging_level_action_group.addAction(self.logging_Level_WARNING_action)
        self.logging_Level_ERROR_action = QAction('ERROR')
        self.logging_Level_ERROR_action.setCheckable(True)
        self.logging_level_action_group.addAction(self.logging_Level_ERROR_action)
        self.logging_Level_CRITICAL_action = QAction('CRITICAL')
        self.logging_Level_CRITICAL_action.setCheckable(True)
        self.logging_level_action_group.addAction(self.logging_Level_CRITICAL_action)

        # 清空日志
        self.clear_errLog_action = QAction('清空日志')

        # 修改scope存储文件名是否加后缀
        self.scope_time_ext_menu = QMenu('scope的时间后缀')
        self.scope_time_ext_menu.setToolTip('启用后会在scope保存的文件命名当中自动加上时间后缀')
        self.scope_time_ext_action_group = QActionGroup(self.scope_time_ext_menu)
        self.scope_time_ext_disable = QAction('禁用')
        self.scope_time_ext_disable.setCheckable(True)
        self.scope_time_ext_action_group.addAction(self.scope_time_ext_disable)
        self.scope_time_ext_enable = QAction('使能')
        self.scope_time_ext_enable.setCheckable(True)
        self.scope_time_ext_action_group.addAction(self.scope_time_ext_enable)

        self.scope_time_ext_format_action = QAction('修改时间后缀格式')
        self.scope_time_ext_format_action.setToolTip('自定义时间格式(年月日时分秒)以及部分字符')


        self.save_host_data_action.setCheckable(True)
        self.save_host_data_action.setEnabled(False)
        self.manage_menu.addAction(self.communication_manage_action)
        self.manage_menu.addAction(self.netCard_config_manage_action)
        self.manage_menu.addSeparator()
        # self.manage_menu.addAction(self.reset_tcp_action)
        self.manage_menu.addAction(self.program_update_action)
        self.manage_menu.addAction(self.upload_struct_data_action)
        self.manage_menu.addSeparator()
        self.manage_menu.addAction(self.com_online_action)
        self.manage_menu.addAction(self.com_offline_action)
        # self.manage_menu.addSeparator()
        # self.manage_menu.addAction(self.save_host_data_action)
        self.manage_menu.addSeparator()
        self.manage_menu.addMenu(self.modify_logging_Level_menu)
        self.modify_logging_Level_menu.addAction(self.logging_Level_DEBUG_action)
        self.modify_logging_Level_menu.addAction(self.logging_Level_INFO_action)
        self.modify_logging_Level_menu.addAction(self.logging_Level_WARNING_action)
        self.modify_logging_Level_menu.addAction(self.logging_Level_ERROR_action)
        self.modify_logging_Level_menu.addAction(self.logging_Level_CRITICAL_action)

        self.manage_menu.addAction(self.clear_errLog_action)

        self.manage_menu.addMenu(self.scope_time_ext_menu)
        self.scope_time_ext_menu.addAction(self.scope_time_ext_disable)
        self.scope_time_ext_menu.addAction(self.scope_time_ext_enable)

        self.scope_time_ext_menu.addSeparator()

        self.scope_time_ext_menu.addAction(self.scope_time_ext_format_action)

        # scope相关
        self.scope_menu = self.menu_bar.addMenu('scope')
        self.scope_start_action = QAction('开始')
        self.scope_start_action.setEnabled(False)
        self.scope_start_action.setCheckable(True)
        self.add_scope_view_action = QAction('添加')
        self.channel_manage_action = QAction('管理')
        self.scope_data_save_action = QAction('保存')
        self.scope_data_save_action.setCheckable(True)
        self.scope_data_save_action.setEnabled(False)
        self.h5_compression_menu = QMenu('压缩')
        self.h5_None_action = QAction('None')
        self.h5_None_action.setCheckable(True)
        self.h5_gzip_action = QAction('gzip')
        self.h5_gzip_action.setCheckable(True)
        # self.h5_compression_menu.addAction('None')
        # self.h5_compression_menu.addAction('gzip')
        self.scope_menu.addAction(self.scope_start_action)
        self.scope_menu.addSeparator()
        self.scope_menu.addAction(self.add_scope_view_action)
        self.scope_menu.addAction(self.channel_manage_action)
        self.scope_menu.addSeparator()
        self.scope_menu.addAction(self.scope_data_save_action)
        self.scope_menu.addMenu(self.h5_compression_menu)
        self.h5_compression_menu.addAction(self.h5_None_action)
        self.h5_compression_menu.addAction(self.h5_gzip_action)
        self.scope_actions = [
            self.scope_start_action, self.add_scope_view_action,
            self.channel_manage_action, self.scope_data_save_action,
            self.h5_compression_menu
        ]

        # E2PROM相关
        self.EEPROM_action = QAction('EEPROM')
        self.menu_bar.addAction(self.EEPROM_action)

        # # 数据库相关
        # self.database_menu = self.menu_bar.addMenu('数据库')
        # self.create_new_database_action = QAction('创建新数据库')
        # self.change_exist_database_action = QAction('切换到已有数据库')
        # self.change_var_table_action = QAction('修改var表')
        # self.database_menu.addAction(self.create_new_database_action)
        # self.database_menu.addAction(self.change_exist_database_action)
        # self.database_menu.addSeparator()
        # self.database_menu.addAction(self.change_var_table_action)

        # 帮助相关
        self.help_action = QAction('帮助')
        self.menu_bar.addAction(self.help_action)

        # 测试相关
        self.test_action = QAction('测试按钮')

    def create_tool_bar(self):
        """ 创造工具栏并添加相应控件 """

        # 创造工具栏
        self.tool_bar = QToolBar(self)
        self.addToolBar(Qt.TopToolBarArea, self.tool_bar)

        # 添加快捷按钮
        # self.tool_bar.addAction(self.load_file_action)
        # self.tool_bar.addSeparator()
        # self.tool_bar.addAction(self.test_action)
        # self.tool_bar.addSeparator()
        self.tool_bar.addAction(self.com_online_action)
        self.tool_bar.addSeparator()
        self.tool_bar.addAction(self.com_offline_action)
        self.tool_bar.addSeparator()
        # self.tool_bar.addAction(self.save_host_data_action)
        # self.tool_bar.addSeparator()
        self.tool_bar.addAction(self.program_update_action)
        self.tool_bar.addSeparator()
        self.tool_bar.addAction(self.load_default_action)
        self.tool_bar.addSeparator()
        self.tool_bar.addAction(self.upload_struct_data_action)
        self.tool_bar.addSeparator()

    def create_status_bar(self):
        """ 创建状态栏 """
        # 创建状态栏
        self.statubar = QStatusBar()
        self.setStatusBar(self.statubar)
        # 展示信息
        self.statubar.showMessage('程序准备就绪', 1000)
        # 设置状态栏里的连接状态
        self.statu_label_conn = QLabel('连接状态:')
        self.statu_label_lamp = QLabel()
        self.statu_label_lamp.setFixedSize(20, 20)
        self.statu_label_lamp.setPixmap(self.pix_red)
        self.statu_label_lamp.setScaledContents(True)
        self.statu_label_spare = QLabel()
        self.statu_label_spare.setFixedSize(200, 20)
        # 设置状态栏控件
        self.statubar.setStyleSheet("QStatusBar::item{border: 0px}")
        self.statubar.addPermanentWidget(self.statu_label_conn)
        self.statubar.addPermanentWidget(self.statu_label_lamp)
        self.statubar.addPermanentWidget(self.statu_label_spare)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    # apply_stylesheet(app, theme='light_teal.xml')     # 优化界面，暂时不用
    # scaleRate = app.screens()[0].logicalDotsPerInch() / 96
    myWin = Main_Window_Ui()  # scaleRate)
    myWin.show()
    # pyinstaller.run
    sys.exit(app.exec())
